define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dingtalk/department',
                    add_url: 'dingtalk/department/add',
                    edit_url: 'dingtalk/department/edit',
                    del_url: 'dingtalk/department/del',
                    detail_url: 'dingtalk/department/detail',
                    user_url: 'dingtalk/department/user',
                    table: 'dingtalk_attendance',
                    //设置不同操作下的弹窗宽高
                    area: {
                        add: ['800px', '450px'],
                        edit: ['800px', '450px'],
                        detail: ['800px', '450px']
                    }
                }
            })

            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'asc',
                pagination: true,
                pageSize: 10,
                commonSearch: false,
                queryParams: function queryParams (params) {
                    var searchForm = $("form[role=form]")
                    if (searchForm.length) {
                        var searchFields = searchForm.serializeArray()
                        for (var i = 0; i < searchFields.length; i++) {
                            if (searchFields[i]['value']) {
                                params[searchFields[i]['name']] = searchFields[i]['value']
                            }
                        }
                    }
                    return params
                },
                queryParamsType: "limit",
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'view-user',
                                    title: '查看部门详情',
                                    icon: 'fa fa-sitemap',
                                    classname: 'btn btn-success btn-xs btn-view-user',
                                    //url: 'dingding/department/user'
                                }
                            ]
                        }
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table)
            Controller.api.bindevent()
            $("#common_search").bind("click", function () {
                table.bootstrapTable('refresh', {
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    pageNumber: 1
                })
            })

            table.on('click', '.btn-view-user', function (e) {
                e.preventDefault()
                var ids = Table.api.selectedids(table)
                var options = table.bootstrapTable('getOptions')
                Fast.api.open(options.extend.user_url + (options.extend.user_url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + ids, __('User'), $(this).data() || {})
            })


        },
        detail: function () {
            // 初始化表格参数配置
            Table.api.init({
                search: false,
                extend: {
                    index_url: 'dingtalk/department/detail/ids/' + $("#code").val(),
                    add_url: 'device/history/add/ids/' + $("#code").val(),
                    edit_url: 'device/history/edit',
                    del_url: 'device/history/del',
                    table: 'dingtalk',
                    //设置不同操作下的弹窗宽高
                    area: {
                        add: ['800px', '450px'],
                        edit: ['800px', '450px']
                    }
                }
            })

            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                pagination: false,
                commonSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'device_code', title: __('Code'), operate: false},
                        {field: 'unit', title: __('Unit'), operate: false},
                        {field: 'contacts', title: __('Contacts'), operate: false},
                        {field: 'contacts_tel', title: __('ContactsTel'), operate: false},
                        {
                            field: 'last_maintain_time',
                            title: __('LastMaintainTime'),
                            formatter: Table.api.formatter.date
                        },
                        {
                            field: 'next_maintain_time',
                            title: __('NextMaintainTime'),
                            formatter: Table.api.formatter.date
                        },
                        {field: 'remark', title: __('Remark'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table)
        },
        user: function () {
            // 初始化表格参数配置
            Table.api.init({
                search: false,
                extend: {
                    index_url: 'dingtalk/department/user/ids/' + $("#code").val(),
                    add_url: 'dingtalk/department/user/' + $("#code").val(),
                    edit_url: 'dingtalk/department/user/edit',
                    del_url: 'dingtalk/department/user/del',
                    table: 'dingtalk_department',
                    //设置不同操作下的弹窗宽高
                    area: {
                        add: ['800px', '450px'],
                        edit: ['800px', '450px']
                    }
                }
            })

            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                pagination: false,
                commonSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'device_code', title: __('Code'), operate: false},
                        {field: 'unit', title: __('Unit'), operate: false},
                        {field: 'contacts', title: __('Contacts'), operate: false},
                        {field: 'contacts_tel', title: __('ContactsTel'), operate: false},
                        {
                            field: 'last_maintain_time',
                            title: __('LastMaintainTime'),
                            formatter: Table.api.formatter.date
                        },
                        {
                            field: 'next_maintain_time',
                            title: __('NextMaintainTime'),
                            formatter: Table.api.formatter.date
                        },
                        {field: 'remark', title: __('Remark'), operate: false},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table)
        },
        add: function () {
            Controller.api.bindevent()
        },
        edit: function () {
            Controller.api.bindevent()
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"))
            }
        }
    }
    return Controller
})